package com._520it.crm.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com._520it.crm.domain.Driver;
import com._520it.crm.service.IDriverService;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DriverController {
	@Autowired
	private IDriverService service;

	@RequestMapping("/driver/index")
	public String index() {
		return "driver/index";
	}

	@RequestMapping("/driver/list/view")
	public ModelAndView listView(ModelAndView modelAndView,
								 @RequestParam(defaultValue = "1") int current,
								 @RequestParam(defaultValue = "10") int size,
								 String name,
								 String sex,
								 String drivingType,
								 String drivingNum,
								 String occupation,
								 @DateTimeFormat(pattern="yyyy-MM-dd") Date birthDate,
								 @DateTimeFormat(pattern="yyyy-MM-dd") Date firstDate

	) {
		modelAndView.setViewName("driver/list");
		List<Driver> driverList = service.selectPage(current,size,name,sex,drivingType,drivingNum,occupation,birthDate,firstDate);

		modelAndView.addObject("list",driverList);
		int count = service.selectPageCount(current,size,name,sex,drivingType,drivingNum,occupation,birthDate,firstDate);
		int totalCount = count%size==0?count/size:count/size+1;
		modelAndView.addObject("totalCount",totalCount);
		modelAndView.addObject("current",current);
		return modelAndView;
	}
}
